package cn.edu.abc.wangzi.fragment;
/*
 *  项目名：Wangzi
 *  包名：cn.edu.abc.wangzi.fragment
 *  创建者：SKFsky
 *  创建时间：2017/8/20 22:57
 *  描述:   美女社区
 */

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.InterfaceAddress;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import cn.edu.abc.wangzi.R;
import cn.edu.abc.wangzi.adapter.GridAdapter;
import cn.edu.abc.wangzi.entity.GirlData;
import cn.edu.abc.wangzi.utils.L;
import cn.edu.abc.wangzi.utils.PicassoUtils;
import cn.edu.abc.wangzi.view.CustomDialog;

public class GrilFragment extends Fragment{

    //列表
    private GridView mGridView;
    //数据
    //private List<GirlData> mList = new ArrayList<>();
    private List<String> mList = new ArrayList<>();
    //适配器
    private GridAdapter mAdapter;
    //提示框
    private CustomDialog dialog;
    //预览图片
    private ImageView iv_img;
    //图片地址的数据
   private List<String> mListUrl = new ArrayList<>();
    //PhotoView
    //private PhotoViewAttacher mAttacher;

    String[] images = new String[]{"http://59.110.228.85:8080/image-s/picture1.jpg","http://59.110.228.85:8080/image-s/picture2.jpg",
            "http://59.110.228.85:8080/image-s/picture3.jpg","http://59.110.228.85:8080/image-s/picture4.jpg",
            "http://59.110.228.85:8080/image-s/picture5.jpg","http://59.110.228.85:8080/image-s/picture6.jpg",
            "http://59.110.228.85:8080/image-s/picture7.jpg","http://59.110.228.85:8080/image-s/picture8.jpg",
            "http://59.110.228.85:8080/image-s/picture9.jpg", "http://59.110.228.85:8080/image-s/picture10.jpg",
            "http://59.110.228.85:8080/image-s/picture11.jpg","http://59.110.228.85:8080/image-s/picture12.jpg",
            "http://59.110.228.85:8080/image-s/picture13.jpg", "http://59.110.228.85:8080/image-s/picture14.jpg",
            "http://59.110.228.85:8080/image-s/picture15.jpg","http://59.110.228.85:8080/image-s/picture16.jpg"};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_girl,null);
        findView(view);
        return view;
    }

    private void findView(View view) {
        mGridView = (GridView) view.findViewById(R.id.mGridView);
        dialog = new CustomDialog(getActivity(), LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT
        ,R.layout.dialog_girl,R.style.Theme_dialog, Gravity.CENTER,R.style.pop_anim_style);
        //初始化数据
        for (int i=0;i<images.length;i++){
            mList.add(images[i]);
        }

        iv_img = (ImageView) dialog.findViewById(R.id.iv_img);
        String welfare = null;
        try {
            //Gank升級 需要转码
            welfare = URLEncoder.encode("福利", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //解析
        RxVolley.get("http://gank.io/api/search/query/listview/category/"+welfare+"/count/50/page/1", new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                L.i("Girl Json:" + t);
                parsingJson(t);
            }
        });

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               // PicassoUtils.loadImaheView(getActivity(),mListUrl.get(i),iv_img);
                PicassoUtils.loadImaheView(getActivity(),mList.get(i),iv_img);
                //iv_img.setImageResource(mList.get(i));
                dialog.show();
            }
        });
    }

    private void parsingJson(String t) {
       /* try {
            JSONObject jsonObject = new JSONObject(t);
            JSONArray jsonArray = jsonObject.getJSONArray("results");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = (JSONObject) jsonArray.get(i);
                String url = json.getString("url");
                mListUrl.add(url);

                GirlData data = new GirlData();
                data.setImgUrl(url);
                mList.add(data);
            }
            mAdapter = new GridAdapter(getActivity(), mList);
            //设置适配器
            mGridView.setAdapter(mAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }*/

        mAdapter = new GridAdapter(getActivity(),mList);
        mGridView.setAdapter(mAdapter);
    }
}
